package cn.iocoder.yudao.module.xkfz.service.excel.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.xkfz.domain.excel.entity.TechnologicalOutputSci;
import cn.iocoder.yudao.module.xkfz.domain.excel.po.TechnologicalOutputSciPo;
import cn.iocoder.yudao.module.xkfz.domain.entity.rules.ScoreCalculationData;
import cn.iocoder.yudao.module.xkfz.mapper.excel.TechnologicalOutputSciMapper;
import cn.iocoder.yudao.module.xkfz.mapper.rules.GroupTypeMapper;
import cn.iocoder.yudao.module.xkfz.service.excel.TechnologicalOutputSciService;
import cn.iocoder.yudao.module.xkfz.service.reporting.ScoreCalculationService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description TODO
 * @Author ZWC
 * @Date 2024/4/29 17:14
 */
@Service
@Log4j2
public class TechnologicalOutputSciServiceImpl implements TechnologicalOutputSciService {

    @Autowired
    private TechnologicalOutputSciMapper technologicalOutputSciMapper;

    @Autowired
    private GroupTypeMapper groupTypeMapper;

    @Autowired
    private ScoreCalculationService scoreCalculationService;

    private  String INDEX_ID = "";
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean importTechnologicalOutputSci(List<TechnologicalOutputSci> list, String year, String indexId) {
        INDEX_ID = indexId;
        technologicalOutputSciMapper.delete(TechnologicalOutputSci::getPeriodTime,year);
        if (CollectionUtil.isNotEmpty(list)){
            for (TechnologicalOutputSci technologicalOutputSci : list) {
                technologicalOutputSci.setId(IdUtil.getSnowflakeNextId());
                technologicalOutputSci.setIndexId(indexId);
                technologicalOutputSci.setPeriodTime(year);
            }
            if (technologicalOutputSciMapper.insertBatch(list,500)) {
                addScoreData(year);
            }
        }
        return true;
    }

    /**
     * 功能描述: 计算某一年份的得分数据并插入到得分表中
     * @Param:
     * @Return: 返回插入得分表的条数
     * @Author: ZWC
     * @Date: 2024/5/20
     */
    private Integer addScoreData(String year){
        List<ScoreCalculationData> countDataList = groupTypeMapper.getSCICount(year);
        Integer num = scoreCalculationService.selectionRule(INDEX_ID, year, countDataList);
        log.info("插入得分表数据条数{}，年份{}",num,year);
        return num;
    }


    @Override
    public PageResult<TechnologicalOutputSci> selectPage(TechnologicalOutputSciPo reqVO) {
        return technologicalOutputSciMapper.selectPage(reqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertTechnologicalOutputSci(TechnologicalOutputSci technologicalOutputSci) {
        if (technologicalOutputSciMapper.insert(technologicalOutputSci) > 0) {
            addScoreData(technologicalOutputSci.getPeriodTime());
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateTechnologicalOutputSci(TechnologicalOutputSci technologicalOutputSci) {
        if (technologicalOutputSciMapper.updateById(technologicalOutputSci) > 0) {
            addScoreData(technologicalOutputSci.getPeriodTime());
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteTechnologicalOutputSci(String id) {
        TechnologicalOutputSci technologicalOutputSci = technologicalOutputSciMapper.selectById(id);
        if (technologicalOutputSciMapper.deleteById(id) > 0) {
            addScoreData(technologicalOutputSci.getPeriodTime());
        }
        return true;
    }


}
